import requests
import http.cookiejar as cookielib
import re

session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename="cookie.txt")

try:
    session.cookies.load(ignore_discard=True)
except:
    print("cookie未能加载")



Agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1"
_header = {
    "Host": "passport.weibo.cn",
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Accept-Encoding": "gzip, deflate, br",
    "referer": "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F%3F%26jumpfrom%3Dweibocom/",
    "Connection": "keep-alive",
    "Origin":"https://passport.weibo.cn",
    "User-Agent": Agent,
    "Content-Type":"application/x-www-form-urlencoded"
}

def gett_index():
    response = session.get("https://m.weibo.cn/p/100103type%3D1%26q%3Dpython?type=all&queryVal=python&luicode=10000011&lfid=106003type%3D1&title=python")
    with open("index_page.html","wb") as f:
        f.write(response.text.encode("utf-8"))
    print("ok")



def sina_wap_login(account, password):
    post_url = "https://passport.weibo.cn/sso/login"
    post_data = {
        "username": account,
        "password": password,
        "savestate": 1,
        "r": "http: // m.weibo.cn /?jumpfrom = wapv4 & tip = 1",
        "ec": 2,
        "pagerefer": "https://passport.weibo.cn/signin/welcome\?entry\=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dwapv4%26tip%3D1",
        "entry": "mweibo",
        "wentry": "",
        "loginfrom": "",
        "client_id": "",
        "code": "",
        "qq": "",
        "mainpageflag": 1,
        "hff": "",
        "hfp": ""
    }
    response_text = session.post(post_url, post_data,headers=_header)

    session.cookies.save()


#sina_wap_login("18200702230","luo467021072")

gett_index()